package edu.aynu.controller;

import edu.aynu.entity.Choice;
import edu.aynu.service.ChoiceService;

import edu.aynu.service.EsChoiceService;
import edu.aynu.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import javax.annotation.Resource;

/**
 * (Choice)表控制层
 *
 * @author haha
 * @since 2021-05-20 14:37:03
 */
@RestController
public class ChoiceController {
    /**
     * 服务对象
     */
    @Resource
    private ChoiceService choiceService;

    @Autowired
    private EsChoiceService esChoiceService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("choice")
    public Result selectOne(Integer id) {
        Choice choice = this.choiceService.queryById(id);
        if (choice == null) {
            return Result.fail();
        }
        return Result.success().add("choice", choice);
    }

    /**
     * 通过实体作为筛选条件查询
     */
    @GetMapping("choices")
    public Result queryAll(@RequestBody Choice choice) {
        List<Choice> list = esChoiceService.queryBySubject(choice);
        //List<Choice> list = this.choiceService.queryAll(choice);
        if (list.size() == 0) {
            return Result.fail();
        }
        return Result.success().add("choices", list);
    }

    /**
     * 新增数据
     */
    @PostMapping("choice")
    public Result insert(@RequestBody Choice choice) {
        int i = this.choiceService.insert(choice);
        if (i == 0) {
            return Result.fail();
        }
        return Result.success();
    }

    /**
     * 修改数据
     */
    @PutMapping("choice")
    public Result update(@RequestBody Choice choice) {
        int i = this.choiceService.update(choice);
        if (i == 0) {
            return Result.fail();
        }
        return Result.success();
    }

    /**
     * 通过主键删除数据
     *
     * @return 是否成功
     */
    @DeleteMapping("choice")
    public Result deleteById(Integer id) {
        boolean b = this.choiceService.deleteById(id);
        if (b == false) {
            return Result.fail();
        }
        return Result.success();
    }
}
